package com.tiennt.android.threeview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AbsoluteLayout.LayoutParams;

@SuppressWarnings("deprecation")
public class SecondView extends Activity {
	AbsoluteLayout mainLayout;
	WebView webView;
	EditText txtAddress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Toast.makeText(SecondView.this, "Second View", Toast.LENGTH_LONG)
				.show();
		initialLayout();
		setupWebView();
	}

	public void initialLayout() {
		mainLayout = new AbsoluteLayout(this);
		setContentView(mainLayout);

		webView = new WebView(this);
		webView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				560, 0, 80));
		mainLayout.addView(webView);
		
		txtAddress = new EditText(this);
		txtAddress.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 80, 0, 0));
		mainLayout.addView(txtAddress);
	}

	public void setupWebView() {
		webView.setWebViewClient(webViewClient);
		webView.loadUrl("http://www.google.com");
		txtAddress.setText("http://www.google.com");
		txtAddress.setEnabled(false);
		txtAddress.setSingleLine();
	}

	WebViewClient webViewClient = new WebViewClient() {

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			Toast.makeText(SecondView.this, "Load completed", Toast.LENGTH_SHORT).show();
			try {
				// AsyncTask
				Thread.sleep(10000);
			} catch (InterruptedException ex) {
				Toast.makeText(getApplicationContext(), "An error occured",
						Toast.LENGTH_LONG).show();
			}
			
			Intent i = new Intent(SecondView.this, ThirdView.class);
			startActivity(i);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			webView.loadUrl(url);
			txtAddress.setText(url);
			return true;
		}

	};
}
